﻿<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>attritems</Title>
      <Author>singba</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>attritems</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>name</ID>
          <ToolTip>属性名称</ToolTip>
          <Default>name</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>itemsType</ID>
          <ToolTip>集合类型</ToolTip>
          <Default>Collection</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>itemType</ID>
          <ToolTip>元素类型</ToolTip>
          <Default>Item</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>parentName</ID>
          <ToolTip>父元素属性名称</ToolTip>
          <Default>Parent</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[#region 属性 $name$
        private $itemsType$ _$name$ = null;
        [XmlArray("$name$"),XmlArrayItem("Item", typeof($itemType$))]
        public $itemsType$ $name$
        {
            get
            {
                if (_$name$ == null)
                {
                    _$name$ = new $itemsType$();
                    _$name$.CollectionChanged += $name$_CollectionChanged;
                }
                return _$name$;
            }
            set
            {
                if (_$name$ != null)
                {
                    _$name$.CollectionChanged -= $name$_CollectionChanged;
                    On$name$Removed(_$name$);
                }
                _$name$ = value;
                RaisePropertyChanged("$name$");
                if (_$name$ != null)
                {
                    _$name$.CollectionChanged += $name$_CollectionChanged;
                    On$name$Added(_$name$);
                }
            }
        }

        private void $name$_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    On$name$Added(e.NewItems);
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    On$name$Removed(e.OldItems);
                    break;
                default:
                    break;
            }
        }
        protected virtual void On$name$Added(IList newItems)
        {
            foreach ($itemType$ item in newItems)
            {
                item.$parentName$ = this;
            }
        }

        protected virtual void On$name$Removed(IList oldItems)
        {
            foreach ($itemType$ item in oldItems)
            {
                item.$parentName$ = null;
            }
        }
        #endregion
	    $end$]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>